<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <a href="#/">首页</a>
  <a href="#/user">用户</a>
  <a href="#/pro">产品</a>
  <div id="content">
    首页
  </div>
</body>
<script>
  // 准备对象,用于封装hash功能
  var router = {
    // 路由存储位置,保存了url与内容处理函数的对应关系
    routes: {},
    // 定义路由规则的方法
    route: function (path, callback) {
      this.routes[path] = callback
    },
    init: function (params) {
      var that = this
      window.onhashchange = function (params) {
        var hash = location.hash.replace('#', '')
        that.routes[hash] && that.routes[hash]()
      }
    }
  }

  var content = document.getElementById("content");

  router.route('/', function () {
    content.innerHTML = '首页';
  })
  router.route('/user', function () {
    content.innerHTML = '用户';
  })
  router.route('/pro', function () {
    content.innerHTML = '产品';
  })

  router.init()
</script>

</html>